// pages/operation/clueManage/clueDetail/index.js
import { StudentModel } from "../../../../api/api-work/student.js"
import Dialog from '@vant/weapp/dialog/dialog';
const studentModel = new StudentModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {}, // 前面传递过来的对象
    userInfo: {}, // 学员信息对象
    name: "", // 学员姓名
    ageName: "", // 年龄
    gradeName: "", // 年级
    gender: "", // 性别
    stage: "", // 级别
    activeTab: "first",

    // 数据源
    firstTableData: [], // 报读课程数据源\
    showClass: false, // 选择班级弹窗
    classOptions: [], // 班级列表
    selectClassRow: {}, // 选择的课程班级
    // 跟进管理
    followList: [],
    // 积分
    integralScore: "",
    integralTotalScord: "",
    integralPageNum: 1,
    integralTotal: 0,
    integralList: [],
    integralLoadStatus: "",
    integralTrigger: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const param = JSON.parse(options.obj)//传递参数
    console.log("pa: ", param)
    if (param.tabIndex == 1) {
      // 报读课程
      this.setData({
        activeTab: "first"
      })
    }
    this.setData({
      query: param
    })

    // // 获取学员信息
    // await this.getStudentInfos()
    // // 获取报读课程
    // await this.getListSecond();
  },

  // 获取学员信息
  async getStudentInfos() {
    studentModel.getStudentApi({ id: this.data.query.id }).then(res => {
      this.setData({
        userInfo: res,
        name: res.name,
        ageName: res.ageName,
        gradeName: res.gradeName,
        stage: res.stage,
        gender: res.gender
      })
    })
  },

  // 监听页面滚动事件
  onPageScroll(e) {
    if (e.scrollTop >= 200) {
      wx.setNavigationBarTitle({
        title: this.data.name
      })
    } else {
      wx.setNavigationBarTitle({
        title: ""
      })
    }
  },

  // 试听
  navigateToTryListen() {
    const param = JSON.stringify({ id: this.data.query.id })
    wx.navigateTo({
      url: `/pages/operation/clueManage/tryListen/index?param=${param}`,
    })
  },

  // 报名续费
  navigateToSignup() {
    const param = JSON.stringify({ id: this.data.query.id })

    wx.navigateTo({
      url: `/pages/operation/signUp/signUpMain/index?param=${param}&&operateType=1`,
    })
  },

  // 查看资料
  navigateToUserInfo() {
    const param = JSON.stringify(this.data.userInfo)
    wx.navigateTo({
      url: `/pages/operation/clueManage/addClue/index?param=${param}`,
    })

  },

  // 获取报读课程
  async getListSecond() {
    studentModel.getListSecondApi({ studentId: this.data.query.id }).then(res => {
      this.setData({
        firstTableData: res
      })
    })
  },

  // 选择报读班级
  selectClassName(e) {
    const row = e.currentTarget.dataset.query;
    this.setData({
      showClass: true,
      selectClassRow: row,
      classOptions: row.studentClassDtos
    })
  },
  // 确定选中班级
  chooseSelect(e) {

    const rows = e.detail;
    if (rows.length == 0) {
      return;
    }
    let params = [];
    rows.forEach(item => {
      let obj = {
        classId: item.id,
        consumeCourseId: this.data.selectClassRow.id,
        studentId: this.data.query.id
      }
      params.push(obj)
    })
    studentModel.addStudentToClassSecondApi(params).then(res => {
      this.getListSecond()
    })
  },
  // 结课
  endClass(e) {
    const row = e.currentTarget.dataset.query;
    if (row.exhaustedButton == 0) {
      return;
    }
    else {
      Dialog.confirm({
        title: '确认结课?',
      }).then(() => {
        studentModel.classEndingApi({ courseId: row.courseId, studentId: this.data.query.id }).then(res => {
          this.getListSecond()
        })
      })

    }
  },
  // 停课
  stopLesson(e) {
    const row = e.currentTarget.dataset.query;
    if (row.stopsResume == 0) {
      return;
    }
    else {
      Dialog.confirm({
        title: '确认停课?',
        message: '停课后，该报读课程将会从班级排课中移除',
      }).then(() => {
        studentModel.stopsAndResumeApi({ courseId: row.courseId, stopsResume: 0, studentId: this.data.query.id }).then(res => {
          this.getListSecond()
        })
      })

    }
  },
  // 复课
  resumeLesson(e) {
    const row = e.currentTarget.dataset.query;
    if (row.stopsResume == 1) {
      return;
    }
    else {
      Dialog.confirm({
        title: '确认复课?',
        message: '复课后，需重新排课',
      }).then(() => {
        studentModel.stopsAndResumeApi({ courseId: row.courseId, stopsResume: 1, studentId: this.data.query.id }).then(res => {
          this.getListSecond()
        })
      })

    }
  },
  // 购课详情
  navigateToLessonDetails(e) {
    const params = JSON.stringify(e.currentTarget.dataset.query);
    wx.navigateTo({
      url: `/pages/operation/student/courseDetails/index?params=${params}`,
    })
  },

  // 上课记录
  navigateToRecord() {
    console.log("调")
    wx.navigateTo({
      url: `/pages/operation/student/studentRecord/index?studentId=${this.data.query.id}`,
    })
  },

  // 获取跟进记录 followListByIdApi
  async followListById() {
    studentModel.followListByIdApi({ studentId: this.data.query.id }).then(res => {
      this.setData({
        followList: res
      })
    })
  },

  // 添加跟进记录
  addFollowRecord() {
    wx.navigateTo({
      url: `/pages/operation/followUpManage/addFollowRecord/index?studentId=${this.data.query.id}`,
    })
  },

  // 获取积分
  async getIntegralCount() {
    studentModel.getIntegralCountApi({ studentId: this.data.query.id }).then(res => {

      this.setData({
        integralScore: res == undefined ? 0 : res.score,
        integralTotalScord: res == undefined ? 0 : res.totalScore
      })
    })
  },
  // 获取积分列表
  async getIntegralList() {
    studentModel.getIntegralListApi({ studentId: this.data.query.id, pageNum: this.data.integralPageNum, pageSize: 15 }).then(res => {
      let rows = this.data.integralList;
      rows.push(...res.rows);
      this.setData({
        integralList: rows,
        integralTotal: res.total
      })
      if (this.data.integralTotal == 0) {
        this.setData({
          integralLoadStatus: ""
        })
      }
      else if (this.data.integralList.length < this.data.integralTotal) {
        this.setData({
          integralLoadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          integralLoadStatus: "nomore"
        })
      }
    })
  },
  getMoreIntegral() {
    if (this.data.integralLoadStatus === "nomore") {
      return;
    }
    this.setData({
      integralPageNum: this.data.integralPageNum + 1,
      integralLoadStatus: "loading"
    })
    this.getIntegralList();
  },
  refreshMoreIntegral() {
    this.setData({
      integralTrigger: false,
      integralPageNum: 1,
      integralList: []
    })
    this.getIntegralList()
  },
  toPointsProvide() {
    wx.navigateTo({
      url: `/pages/operation/points/pointsProvide/index?param=${this.data.query.id}&delta=1`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {

    this.setData({
      integralTrigger: false,
      integralPageNum: 1,
      integralList: []
    })
    // 获取学员信息
    await this.getStudentInfos()
    // 获取报读课程
    await this.getListSecond();
    // 获取跟进管理
    await this.followListById();
    // 积分
    await this.getIntegralCount();
    // 积分列表
    await this.getIntegralList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})